#include "ArrowButton.h"
#include <QEnterEvent>
#include <QResizeEvent>

ArrowButton::ArrowButton(ArrowDirection direction, QWidget* parent)
    : QPushButton(parent),
      m_direction(direction),
      m_borderColor(Qt::black),  // 边框颜色设置为黑色
      m_borderWidth(2),
      m_arrowColor(Qt::black),  // 箭头颜色设置为黑色
      m_isHovered(false)
{
    setFixedSize(80, 80);
    setCursor(Qt::PointingHandCursor);
    setAttribute(Qt::WA_Hover);

    // 设置按钮背景为透明
    setStyleSheet("background-color: transparent;");
    // 生成缓存图像
    generateCache();
}

void ArrowButton::setDirection(ArrowDirection direction)
{
    if (m_direction != direction)
    {
        m_direction = direction;
        generateCache();
        update();
    }
}

void ArrowButton::setBorderColor(const QColor& color)
{
    if (m_borderColor != color)
    {
        m_borderColor = color;
        generateCache();
        update();
    }
}

void ArrowButton::setBorderWidth(int width)
{
    if (m_borderWidth != width && width > 0)
    {
        m_borderWidth = width;
        generateCache();
        update();
    }
}

void ArrowButton::setArrowColor(const QColor& color)
{
    if (m_arrowColor != color)
    {
        m_arrowColor = color;
        generateCache();
        update();
    }
}

void ArrowButton::generateCache()
{
    // 创建与按钮相同大小的图像
    m_cache = QPixmap(size());
    m_cache.fill(Qt::transparent);

    QPainter painter(&m_cache);
    painter.setRenderHint(QPainter::Antialiasing, true);

    painter.setPen(Qt::NoPen);
    painter.setBrush(m_arrowColor);

    int arrowSize = qMin(width(), height()) * 0.4;  // 根据按钮大小计算箭头大小
    int centerX = width() * 0.5;                    // 计算箭头的中心点
    int centerY = height() * 0.5;                   // 计算箭头的中心点

    switch (m_direction)
    {
        case ArrowDirection::Up:
        case ArrowDirection::Down:
            // 绘制上下
            drawSingleArrow(painter, centerX, centerY, arrowSize);
            break;
        case ArrowDirection::Left:
            drawCurvedArrow(painter, centerX, centerY, arrowSize, true);
            break;
        case ArrowDirection::Right:
            drawCurvedArrow(painter, centerX, centerY, arrowSize, false);
            break;
        case ArrowDirection::LeftShift:
            drawShiftArrow(painter, centerX, centerY, arrowSize, true);
            break;
        case ArrowDirection::RightShift:
            drawShiftArrow(painter, centerX, centerY, arrowSize, false);
            break;
    }
}

void ArrowButton::drawShiftArrow(QPainter& painter, int centerX, int centerY, int arrowSize, bool isLeft)
{
    // 简单弯折箭头：只绘制带角度的箭头
    if (isLeft)
    {
        // 左转箭头（45度角）
        QPolygonF arrow;
        arrow << QPointF(centerX - arrowSize / 2, centerY) << QPointF(centerX, centerY - arrowSize / 2) << QPointF(centerX + arrowSize / 4, centerY - arrowSize / 4) << QPointF(centerX, centerY) << QPointF(centerX + arrowSize / 4, centerY + arrowSize / 4)
              << QPointF(centerX, centerY + arrowSize / 2);
        painter.drawPolygon(arrow);
    }
    else
    {
        // 右转箭头（45度角）
        QPolygonF arrow;
        arrow << QPointF(centerX + arrowSize / 2, centerY) << QPointF(centerX, centerY - arrowSize / 2) << QPointF(centerX - arrowSize / 4, centerY - arrowSize / 4) << QPointF(centerX, centerY) << QPointF(centerX - arrowSize / 4, centerY + arrowSize / 4)
              << QPointF(centerX, centerY + arrowSize / 2);
        painter.drawPolygon(arrow);
    }
}

// void ArrowButton::drawSingleArrow(QPainter& painter, int centerX, int centerY, int arrowSize)
//{
//     switch (m_direction)
//     {
//         case ArrowDirection::Up:
//         {
//             QPolygonF triangle;
//             triangle << QPointF(centerX, centerY - arrowSize / 2) << QPointF(centerX - arrowSize / 2, centerY) << QPointF(centerX + arrowSize / 2, centerY);
//             painter.drawPolygon(triangle);
//             QRect shaftRect(centerX - arrowSize / 4, centerY, arrowSize / 2, arrowSize / 2);
//             painter.drawRect(shaftRect);
//             break;
//         }
//         case ArrowDirection::Down:
//         {
//             QPolygonF triangle;
//             triangle << QPointF(centerX, centerY + arrowSize / 2) << QPointF(centerX - arrowSize / 2, centerY) << QPointF(centerX + arrowSize / 2, centerY);
//             painter.drawPolygon(triangle);
//             QRect shaftRect(centerX - arrowSize / 4, centerY - arrowSize / 2, arrowSize / 2, arrowSize / 2);
//             painter.drawRect(shaftRect);
//             break;
//         }
//         default:
//             break;
//     }
// }
//
// void ArrowButton::drawShiftArrow(QPainter& painter, int centerX, int centerY, int arrowSize, bool isLeft)
//{
//     // 绘制平移箭头（单箭头+方框）
//     if (isLeft)
//     {
//         // 左平移
//         QPolygonF triangle;
//         triangle << QPointF(centerX - arrowSize / 2, centerY) << QPointF(centerX, centerY - arrowSize / 2) << QPointF(centerX, centerY + arrowSize / 2);
//         painter.drawPolygon(triangle);
//
//         // 方框
//         QRect boxRect(centerX, centerY - arrowSize / 4, arrowSize / 2, arrowSize / 2);
//         painter.drawRect(boxRect);
//     }
//     else
//     {
//         // 右平移
//         QPolygonF triangle;
//         triangle << QPointF(centerX + arrowSize / 2, centerY) << QPointF(centerX, centerY - arrowSize / 2) << QPointF(centerX, centerY + arrowSize / 2);
//         painter.drawPolygon(triangle);
//
//         // 方框
//         QRect boxRect(centerX - arrowSize / 2, centerY - arrowSize / 4, arrowSize / 2, arrowSize / 2);
//         painter.drawRect(boxRect);
//     }
// }

void ArrowButton::drawCurvedArrow(QPainter& painter, int centerX, int centerY, int arrowSize, bool isLeft)
{
    // 计算方框宽度和高度
    int boxWidth = arrowSize * 0.5;
    int boxHeight = arrowSize * 0.3;

    if (isLeft)
    {
        // 左平移
        QPolygonF triangle;
        triangle << QPointF(centerX - arrowSize / 2, centerY) << QPointF(centerX, centerY - arrowSize / 2) << QPointF(centerX, centerY + arrowSize / 2);
        painter.drawPolygon(triangle);

        // 方框 - 使用boxHeight控制高度
        QRect boxRect(centerX, centerY - boxHeight / 2, arrowSize / 2, boxHeight);
        painter.drawRect(boxRect);
    }
    else
    {
        // 右平移
        QPolygonF triangle;
        triangle << QPointF(centerX + arrowSize / 2, centerY) << QPointF(centerX, centerY - arrowSize / 2) << QPointF(centerX, centerY + arrowSize / 2);
        painter.drawPolygon(triangle);

        // 方框 - 使用boxHeight控制高度
        QRect boxRect(centerX - arrowSize / 2, centerY - boxHeight / 2, arrowSize / 2, boxHeight);
        painter.drawRect(boxRect);
    }
}

void ArrowButton::drawSingleArrow(QPainter& painter, int centerX, int centerY, int arrowSize)
{
    // 计算方框宽度和高度
    int boxWidth = arrowSize * 0.3;
    int boxHeight = arrowSize * 0.5;

    switch (m_direction)
    {
        case ArrowDirection::Up:
        {
            QPolygonF triangle;
            triangle << QPointF(centerX, centerY - arrowSize / 2) << QPointF(centerX - arrowSize / 2, centerY) << QPointF(centerX + arrowSize / 2, centerY);
            painter.drawPolygon(triangle);

            // 方框 - 使用boxHeight控制高度
            QRect shaftRect(centerX - boxWidth / 2, centerY, boxWidth, boxHeight);
            painter.drawRect(shaftRect);
            break;
        }
        case ArrowDirection::Down:
        {
            QPolygonF triangle;
            triangle << QPointF(centerX, centerY + arrowSize / 2) << QPointF(centerX - arrowSize / 2, centerY) << QPointF(centerX + arrowSize / 2, centerY);
            painter.drawPolygon(triangle);

            // 方框 - 使用boxHeight控制高度
            QRect shaftRect(centerX - boxWidth / 2, centerY - boxHeight, boxWidth, boxHeight);
            painter.drawRect(shaftRect);
            break;
        }
        default:
            break;
    }
}

void ArrowButton::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    // 设置按钮背景为透明
    QColor backgroundColor = Qt::transparent;

    // 只有在悬停或按下时才显示背景色
    if (isDown())
    {
        backgroundColor = QColor(200, 200, 200, 120);  // 半透明灰色
    }
    else if (m_isHovered)
    {
        backgroundColor = QColor(220, 220, 220, 50);  // 半透明浅灰色
    }

    // 绘制按钮背景（透明或半透明）
    QRect innerRect = rect().adjusted(m_borderWidth, m_borderWidth, -m_borderWidth, -m_borderWidth);
    painter.setBrush(backgroundColor);
    painter.setPen(Qt::NoPen);
    painter.drawRoundedRect(innerRect, 8, 8);

    // 绘制边框（使用与按钮相同的颜色，这里用黑色）
    painter.setBrush(Qt::NoBrush);
    painter.setPen(QPen(m_borderColor, m_borderWidth));
    painter.drawRoundedRect(innerRect, 8, 8);

    // 绘制缓存的箭头图像
    painter.drawPixmap(0, 0, m_cache);
}

bool ArrowButton::event(QEvent* event)
{
    switch (event->type())
    {
        case QEvent::HoverEnter:
            m_isHovered = true;
            update();
            break;
        case QEvent::HoverLeave:
            m_isHovered = false;
            update();
            break;
        default:
            break;
    }
    return QPushButton::event(event);
}

void ArrowButton::resizeEvent(QResizeEvent* event)
{
    QPushButton::resizeEvent(event);
    // 大小改变时重新生成缓存
    generateCache();
}